# -*- coding: utf-8 -*-

'''
Copyright (C) 2011 Daniele Simonetti

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'''


### PySide ###
from PySide import QtCore, QtGui

class EpItemDelegate(QtGui.QStyledItemDelegate):
    def __init__(self, parent=None):
        super(EpItemDelegate, self).__init__(parent)
   
    #QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) c
    def paint(self, painter, option, index):
        if not index.isValid():
            super(EpItemDelegate, self).paint(painter, option, index)
            return
        
        item = index.data(QtCore.Qt.UserRole)
        text_color = index.data(QtCore.Qt.ForegroundRole)
        bg_color = index.data(QtCore.Qt.BackgroundRole)
        hg_color = QtGui.QBrush(bg_color)
        hg_color.setStyle(QtCore.Qt.Dense3Pattern)
        
        painter.save()
        
        # fill the background color
        if option.state & QtGui.QStyle.State_Selected == QtGui.QStyle.State_Selected:
             painter.fillRect(option.rect, option.palette.highlight())
             text_color = option.palette.highlightedText()
        else:
            painter.fillRect(option.rect, bg_color)
        
        painter.setPen(QtCore.Qt.DashLine)
        painter.drawLine(option.rect.bottomLeft(), option.rect.bottomRight())
        
        main_font = painter.font()
        sub_font = QtGui.QFont().resolve(main_font)
        sub_font.setPointSize(7)
        
        # paint the airdate with a smaller font over the item name
        # suppose to have 24 pixels in vertical        
        painter.setFont(sub_font)
        font_metric = painter.fontMetrics()
        ep_dt = str(item.airdate)
        ep_dt_rect = font_metric.boundingRect(ep_dt)
        
        text_pen = QtGui.QPen(text_color, 1)
        
        painter.setPen(text_pen)
        painter.drawText(option.rect.left(), option.rect.top() + ep_dt_rect.height(), ep_dt)
        
        # paint the episode title
        painter.setFont(main_font)
        font_metric = painter.fontMetrics()
        ep_text = unicode(item)
        ep_tx_rect = font_metric.boundingRect(ep_text)
        
        painter.drawText(option.rect.left(), option.rect.top() + ep_tx_rect.height() + ep_dt_rect.height(), ep_text)
        
        painter.restore()
